package com.itheima.controller.backend.Service.ServiceImpl;

import com.itheima.VO.ReportDataVO;
import com.itheima.VO.ReportVO;
import com.itheima.common.utils.DateUtils;
import com.itheima.controller.backend.Service.ReportService;
import com.itheima.mapper.ReportMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.ReportData;
import com.itheima.pojo.SetmealCount;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;




    @Override
    public Map getMemberReport(){
        Map<String, List<String>> map = new HashMap<>();
        List<String> regTimeList = reportMapper.selectRegTimeList();
        log.info("regTimeList：{}",regTimeList);
        for (String regTime : regTimeList) {
            String[] split = regTime.split("-");
            String year = split[0];
            String month = split[1];
            String day = split[2];
            regTime = year+"."+month+"."+day;
        }

        List<String> memberCount = new ArrayList<>();
        for (String regTime : regTimeList) {
            Integer number = 0;
            for (String t : regTimeList) {
                if (regTime.equals(t)){
                    number++;
                }
            }
            memberCount.add(number.toString());
        }


        map.put("months",regTimeList);
        map.put("memberCount",memberCount);
        return map;
    }



    @Override
    public ReportVO getSetmealReport(){
        ReportVO reportVO = new ReportVO();

        List<String> setmealNames = reportMapper.selectSetmealNames();
        reportVO.setSetmealName(setmealNames);

        List<Integer> setmealIds = reportMapper.selectIds();
       List<SetmealCount> setmealCount = new ArrayList<>();
        for (Integer setmealId : setmealIds) {
            Integer value = reportMapper.selectCountBySetmealId(setmealId);
            String name = reportMapper.selectSetmealNameById(setmealId);
            SetmealCount setmealCount1 = new SetmealCount();
            setmealCount1.setName(name);
            setmealCount1.setValue(value);
            setmealCount.add(setmealCount1);
        }
        reportVO.setSetmealCount(setmealCount);




        return reportVO;
    }


    @Override
    public ReportDataVO getBusinessReportData(){
        ReportDataVO reportDataVO = new ReportDataVO();

        List<ReportData> reportDataList = reportMapper.selectReportData();
        reportDataVO.setHotSetmeal(reportDataList);

        //今日到诊数
        LocalDate begin = LocalDate.now();
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(begin, LocalTime.MAX);

        reportDataVO.setTodayVisitsNumber(reportMapper.selectTodayVisitsNumber(beginTime, endTime));

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String reportData = simpleDateFormat.format(DateUtils.getToday());
        reportDataVO.setReportDate(reportData);

        //TODO 可以优化（有时间再搞，三个mapper方法可以合成一个完成）
        reportDataVO.setTodayNewMember(reportMapper.selectTodayNewMember(beginTime, endTime));

        Date now = DateUtils.getToday();
        Date beginWeekTime = DateUtils.getFirstDayOfWeek(now);
        Date endWeekTime = DateUtils.getLastDayOfWeek(now);
        reportDataVO.setThisWeekNewMember(reportMapper.selectThisWeekNewMember(beginWeekTime, endWeekTime));


        Date beginMonthTime = DateUtils.getFirstDay4ThisMonth();
        Date endMonthTime = DateUtils.getToday();
        reportDataVO.setThisMonthNewMember(reportMapper.selectThisMonthNewMember(beginMonthTime, endMonthTime));

        reportDataVO.setThisWeekVisitsNumber(reportMapper.selectThisWeekVisitsNumber(beginWeekTime, endWeekTime));

        reportDataVO.setTotalMember(reportMapper.selectTotalMember());

        reportDataVO.setThisMonthOrderNumber(reportMapper.selectThisMonthOrderNumber(beginMonthTime,endMonthTime));

        reportDataVO.setThisMonthVisitsNumber(reportMapper.selectThisMonthVisitsNumber(beginMonthTime,endMonthTime));

        reportDataVO.setTodayOrderNumber(reportMapper.selectTodayOrderNumber(beginTime,endTime));

        reportDataVO.setThisWeekOrderNumber(reportMapper.selectThisWeekOrderNumber(beginWeekTime,endWeekTime));


        return reportDataVO;
    }


    @Override
    public void excel(HttpServletResponse response) throws Exception{
        FileInputStream in = new FileInputStream(new File("D:\\JavaCode\\bxg-health-parent\\bxg-health-backend\\src\\main\\resources\\templates\\report_template.xlsx"));
        XSSFWorkbook excel = new XSSFWorkbook(in);

        XSSFSheet sheet = excel.getSheet("Sheet1");
        String Today = DateUtils.parseDate2String(DateUtils.getToday());
        sheet.getRow(2).getCell(5).setCellValue(Today);


        LocalDate begin = LocalDate.now();
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(begin, LocalTime.MAX);
        sheet.getRow(4).getCell(5).setCellValue(reportMapper.selectTodayNewMember(beginTime,endTime));
        sheet.getRow(4).getCell(7).setCellValue(reportMapper.selectTotalMember());

        sheet.getRow(5).getCell(5).setCellValue(reportMapper.selectThisWeekNewMember(DateUtils.getFirstDayOfWeek(DateUtils.getToday()),DateUtils.getLastDayOfWeek(DateUtils.getToday())));
        sheet.getRow(5).getCell(7).setCellValue(reportMapper.selectThisMonthNewMember(DateUtils.getFirstDay4ThisMonth(),DateUtils.getToday()));

        sheet.getRow(7).getCell(5).setCellValue(reportMapper.selectTodayOrderNumber(beginTime,endTime));
        sheet.getRow(7).getCell(7).setCellValue(reportMapper.selectTodayVisitsNumber(beginTime,endTime));

        sheet.getRow(8).getCell(5).setCellValue(reportMapper.selectThisWeekOrderNumber(DateUtils.getFirstDayOfWeek(DateUtils.getToday()),DateUtils.getLastDayOfWeek(DateUtils.getToday())));
        sheet.getRow(8).getCell(7).setCellValue(reportMapper.selectThisWeekVisitsNumber(DateUtils.getFirstDayOfWeek(DateUtils.getToday()),DateUtils.getLastDayOfWeek(DateUtils.getToday())));

        sheet.getRow(9).getCell(5).setCellValue(reportMapper.selectThisMonthOrderNumber(DateUtils.getFirstDay4ThisMonth(),DateUtils.getToday()));
        sheet.getRow(9).getCell(7).setCellValue(reportMapper.selectThisMonthVisitsNumber(DateUtils.getFirstDay4ThisMonth(),DateUtils.getToday()));

        List<ReportData> reportDataList = reportMapper.selectReportData();
        for (int i = 0; i < 4; i++) {
            sheet.getRow(12+i).getCell(4).setCellValue(reportDataList.get(i).getName());
            sheet.getRow(12+i).getCell(5).setCellValue(reportDataList.get(i).getSetmeal_count());
            sheet.getRow(12+i).getCell(6).setCellValue(reportDataList.get(i).getProportion());
        }

        log.info("excel导出成功");
        ServletOutputStream out = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("content-Disposition", "attachment;filename=report.xlsx");
        excel.write(out);

        out.close();
        excel.close();
    }

}
